package com.sky.business.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.business.sys.domain.MQTTMessage;
import com.sky.business.sys.param.MQTTMessageQueryListParam;
import com.sky.business.sys.service.MQTTMessageService;
import com.sky.common.api.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "MQTT消息管理", description = "MQTT消息记录控制层")
@RequestMapping("/mqttMessage")
@Validated
public class MQTTMessageController {

    @Autowired
    private MQTTMessageService mqttMessageService;


    @Operation(summary = "MQTT消息列表")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Map<String, Object>> list(@RequestBody MQTTMessageQueryListParam param){

        LambdaQueryWrapper<MQTTMessage> queryWrapper = new LambdaQueryWrapper<>();

        if(param.getStartTime() != null){
            queryWrapper.ge(MQTTMessage::getCreateDate, param.getStartTime());
        }
        if(param.getEndTime() != null){
            queryWrapper.le(MQTTMessage::getCreateDate, param.getEndTime());
        }
        if(param.getType() != null){
            queryWrapper.eq(MQTTMessage::getType, param.getType());
        }
        if(StringUtils.isNotBlank(param.getContent())){
            queryWrapper.like(MQTTMessage::getMessage, param.getContent());
        }
        if(StringUtils.isNotBlank(param.getEvent())){
            queryWrapper.eq(MQTTMessage::getEvent, param.getEvent());
        }
        if(StringUtils.isNotBlank(param.getDeviceId())){
            queryWrapper.likeLeft(MQTTMessage::getDeviceId, param.getDeviceId());
        }
        if(StringUtils.isNotBlank(param.getTaskId())){
            queryWrapper.eq(MQTTMessage::getTaskId, param.getTaskId());
        }
        if(param.getNodeId() != null && param.getNodeId() > 0){
            queryWrapper.eq(MQTTMessage::getNodeId, param.getNodeId());
        }

        queryWrapper.orderByDesc(MQTTMessage::getCreateDate);
        IPage<MQTTMessage> iPage = mqttMessageService.page(new Page<>(param.getPage(), param.getLimit()), queryWrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("records", iPage.getRecords());
        data.put("total", iPage.getTotal());
        return Result.success(data);
    }

}
